/* ====================================================================
 * (#)Header: MessageListenerBean.java
 * (#)Revision: 
 * (#)Date Created: 29-Oct-09 12:47:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms.mdb;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

/**
 * <code>MessageListenerBean</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
@MessageDriven(name = "MessageListenerBean", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/ForumChannel") })
public class MessageListenerBean implements MessageListener {

	@Resource
	private MessageDrivenContext context;

	public MessageListenerBean() {

	}

	public void onMessage(Message message) {

		if (message instanceof TextMessage) {
			onTextMessage((TextMessage) message);
		} else if (message instanceof ObjectMessage) {
			onObjectMessage((ObjectMessage) message);
		} else {
			System.out.println(this + "  received message: " + message);
		}
	}

	protected void onTextMessage(TextMessage message) {
		try {
			System.out.println(this + "  received TextMessage: " + message.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void onObjectMessage(ObjectMessage message) {
		try {
			System.out.println(this + "  received ObjectMessage: " + message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// === callbacks === //
	@PreDestroy
	public void remove() {
		System.out.println(this + " is being destroyed.");
	}

	// === get/set === //
	public MessageDrivenContext getContext() {
		return context;
	}

	public void setContext(MessageDrivenContext context) {
		this.context = context;
	}
}
